﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Common;
using System.IO;

namespace ZX.WebApp.AjaxData
{
    public partial class UploadFile : ZX.Web.UI.Controls.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetStringFromQueryString("type", "") != "")
            {
                string[] filext = { "exe", "dll", "aspx", "asp", "jar", "com", "bat","php","jsp" };
                HttpPostedFile postfile = ZXContext.Request.Files[0];
                Response.Clear();
                if (Request.Files.Count > 0 && !ZX.Common.Globals.IIFExistExtension(postfile.FileName, filext))
                {
                    string output = @"<script type=""text/javascript"">window.parent.CKEDITOR.tools.callFunction({0} ,'{1}');</script>";
                    System.Web.HttpPostedFile f = Request.Files["upload"];
                    string filename = "/Upload/" + System.Guid.NewGuid().ToString() + f.FileName.Substring(f.FileName.LastIndexOf("."));
                    f.SaveAs(Server.MapPath("~" + filename));
                    output = string.Format(output, Request["CKEditorFuncNum"], filename);
                    Response.Write(output);
                    Response.End();
                }
                else
                {
                    Response.Write("no file");
                    Response.End();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string[] filext = { "exe", "dll", "aspx", "asp", "jar", "com", "bat", "php", "jsp" };
            HttpPostedFile postfile = ZXContext.Request.Files[0];
            if (postfile != null && !ZX.Common.Globals.IIFExistExtension(postfile.FileName, filext))//
            {
                int fileSize = (int)postfile.ContentLength / 1024 / 1024;
                if (fileSize > 50)
                {
                    ZXContext.Response.Write(string.Format("<script language='Javascript'>parent.FileSize('{0}');</script>", fileSize));
                    return;
                }
                else
                {
                    string str = string.Format("F" + DateTime.Now.ToString("yyyyMMddHHss") + ".{1}", this.UserName, ZX.Common.Globals.GetExtension(postfile.FileName));
                    string kbytes = "0KB";
                    //判断是否存在
                    if (!Directory.Exists(ZXContext.Request.PhysicalApplicationPath + "upload"))
                    {
                        Directory.CreateDirectory(ZXContext.Request.PhysicalApplicationPath + "upload");//创建新路径
                    }
                    postfile.SaveAs(ZXContext.Request.PhysicalApplicationPath + "upload/" + str);
                    if (postfile.ContentLength / 1024 < 1024)
                    {
                        kbytes = Convert.ToString(postfile.ContentLength / 1024) + "KB";
                    }
                    else
                    {

                        kbytes = string.Format("{0:#0.00}", (float)postfile.ContentLength / 1024 / 1024) + "MB";
                    }
                    string id = this.GetStringFromQueryString("c", "");
                    ZXContext.Response.Write(string.Format("<script language='Javascript'>parent.onUpload('{0}','{1}');parent.onUpload('{2}','{3}');parent.onUpload('Size','{3}');parent.onUpload('Caption','" + GetRealFileName(postfile.FileName) + "');parent.closeUpload_div();</script>", id, str, "TrSize", kbytes));
                }
            }
        }

        /// <summary> 
        /// 功能：获取文件名 
        /// </summary> 
        /// <param name="strPathName">文件路径</param> 
        /// <returns>文件名</returns> 
        private string GetRealFileName(string strPathName) 
        { 
            string [] strFileName = strPathName.Split('\\'); 
            int iPos = strFileName.GetUpperBound(0); 
            return strFileName[iPos]; 
        }
    }
}
